package stuttgart.uni.locationtracker;

import java.text.DecimalFormat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public  class ConnectFragment extends Fragment {
	
		//Strange Bug if Variables are not static
	
		public static Button start;
		public static Button logout;
		
		private static EditText username;
		private static EditText server;
		
		private static TextView packetReductionRatio;
		
		private  static OnClickListener startbuttonListener;
		private  static OnClickListener logoutButtonListener;
		
		private  static DecimalFormat ratioFormat;

		public ConnectFragment() {
		}

		public void updatePacketReductionRatio(double packetReductionRatio){
			System.out.println("Connect Fragment updatePacketReductionRatio packetReductionRatio = "+this.packetReductionRatio);
			if(this.packetReductionRatio!=null){
				this.packetReductionRatio.setText(ratioFormat.format(packetReductionRatio));
			}
		}
		
		

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			
			start = (Button)getActivity().findViewById(R.id.button_start);
			logout = (Button)getActivity().findViewById(R.id.button_logout);
			
			username = (EditText) getActivity().findViewById(R.id.edittext_username);
			server = (EditText) getActivity().findViewById(R.id.edittext_server);
			
			packetReductionRatio = (TextView) getActivity().findViewById(R.id.text_packet_ratio);
			
			if(((LocationTrackerActivity)getActivity()).running){
				logout.setEnabled(true);
				start.setEnabled(false);
			}
			else{
				start.setEnabled(true);
				logout.setEnabled(false);
			}
			
			startbuttonListener = new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent startClientIntent = new Intent(getActivity(),LocationTrackerService.class);
					startClientIntent.putExtra("SERVER_IP_ADDRESS",server.getText().toString().trim());
					startClientIntent.putExtra("USERNAME",username.getText().toString().trim());
					getActivity().startService(startClientIntent);
					((LocationTrackerActivity)getActivity()).running = getActivity().bindService(new Intent(getActivity(),LocationTrackerService.class),((LocationTrackerActivity)getActivity()).mConnection, Context.BIND_AUTO_CREATE);
					if(((LocationTrackerActivity)getActivity()).running){
						logout.setEnabled(true);
						start.setEnabled(false);
					}
					else{
						start.setEnabled(true);
						logout.setEnabled(false);
					}
				}
			};
			
			logoutButtonListener = new OnClickListener() {

				@Override
				public void onClick(View v) {

					if(((LocationTrackerActivity)getActivity()).mService!=null){
						try {
							((LocationTrackerActivity)getActivity()).mService.requestLogout();
							Log.d("LocationTrackerActivity","requestLogout()");
						} catch (RemoteException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						getActivity().unbindService(((LocationTrackerActivity)getActivity()).mConnection);
						getActivity().stopService(new Intent(getActivity(),LocationTrackerService.class));
						logout.setEnabled(false);
						start.setEnabled(true);
					}
				}
			};
			start.setOnClickListener(startbuttonListener);
			logout.setOnClickListener(logoutButtonListener);
			
			if(((LocationTrackerActivity)getActivity()).running){
				start.setEnabled(false);
	        	logout.setEnabled(true);
	        }
	        else{
	        	start.setEnabled(true);
	        	logout.setEnabled(false);
	        }
			
			ratioFormat = new DecimalFormat("#.00%");
			System.out.println("Connect Fragment onActivityCreate ConnectFragment");
			System.out.println("Connect Fragment onActivityCreate packetReductionRatio = "+this.packetReductionRatio);
		}


		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.connect_fragment_layout, null);
			return v;
		}
	}